﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using Intelligent.OTC.Domain.DataModel;
using Intelligent.OTC.Common.UnitOfWork;
using Intelligent.OTC.Domain;
using Intelligent.OTC.Business;
using Intelligent.OTC.Domain.Repositories;
using Intelligent.OTC.Common;
using System.Configuration;
using System.IO;
using Intelligent.OTC.Common.Utils;
using Intelligent.OTC.WebApi.Core;
using Intelligent.OTC.Common.Attr;
using Intelligent.OTC.Common.Exceptions;

namespace Intelligent.OTC.WebApi.Controllers
{
    [UserAuthorizeFilter(actionSet: "master")]
    public class ContactController : ApiController
    {
        string dear = AppContext.Current.User.Deal.ToString();
        [HttpGet]
        public IEnumerable<Contactor> Get(string customerCode)
        {
            //strDeal = dear;
            ContactService service = SpringFactory.GetObjectImpl<ContactService>("ContactService");
            var collectorList = service.GetContactByCustomer(customerCode);
            return collectorList.AsQueryable<Contactor>();

        }

        [HttpGet]
        public IEnumerable<Contactor> GetContact(string customerNums)
        {
            //strDeal = dear;
            ContactService service = SpringFactory.GetObjectImpl<ContactService>("ContactService");
            var collectorList = service.GetContactsByCustomers(customerNums);
            return collectorList.AsQueryable<Contactor>();
        }

        [HttpPost]
        public void delete(int id)
        {
            ContactService service = SpringFactory.GetObjectImpl<ContactService>("ContactService");
            Helper.Log.Info(id);
            service.DeleteContact(id);
        }

        //[HttpPut]
        //public void Put([FromBody]Contactor cust)
        //{
        //    ContactService service = SpringFactory.GetObjectImpl<ContactService>("ContactService");
        //    service.AddOrUpdateContact(cust);
        //}

        [HttpPost]
        public void Post([FromBody] Contactor cust)
        {
            ContactService service = SpringFactory.GetObjectImpl<ContactService>("ContactService");
            try
            {
                service.AddOrUpdateContact(cust);
            }
            catch (OTCServiceException ex)
            {
                throw new OTCServiceException(ex.Message);
            }
            catch
            {
                throw new OTCServiceException("Add Or Update Error!");
            }
            
        }
        [HttpPost]
        public void Post(string type, [FromBody] ContactorDomain cont)
        {
            ContactService service = SpringFactory.GetObjectImpl<ContactService>("ContactService");
            service.AddOrUpdateDomain(cont);
        }

        [HttpPost]
        public void deleteDomain(int domainid)
        {
            ContactService service = SpringFactory.GetObjectImpl<ContactService>("ContactService");
            Helper.Log.Info(domainid);
            service.DeleteDomain(domainid);
        }

        //[HttpPost]
        //public void insertInvoiceLog(string cusNum, int invId, string messId, string conPerson, string comms)
        //{
        //    ContactService service = SpringFactory.GetObjectImpl<ContactService>("ContactService");
        //    service.insertInvoiceLogForNotice(cusNum, invId, messId, conPerson, comms);
        //}

        //[HttpPost]
        //public void insertInvoiceLog(string cusNum, int invId, string messId, string conPerson, string comms, DateTime promissDate)
        //{
        //    ContactService service = SpringFactory.GetObjectImpl<ContactService>("ContactService");
        //    service.insertInvoiceLogForPtp(cusNum, invId, messId, conPerson, comms, promissDate);
        //}

        //[HttpPost]
        //public void insertInvoiceLog(string cusNum, int invId, string messId, string conPerson, string comms, string disputeReason, string type)
        //{
        //    ContactService service = SpringFactory.GetObjectImpl<ContactService>("ContactService");
        //    service.insertInvoiceLogForDispute(cusNum, invId, messId, conPerson, comms, disputeReason);
        //}

    }
}